import 'dart:io';

import 'package:app_30/home/fragment/vm/vip_vm.dart';
import 'package:app_30/res/colors.dart';
import 'package:app_30/util/cache_utils.dart';
import 'package:app_30/util/dialog_utils.dart';
import 'package:app_30/widgets/my_app_bar.dart';
import 'package:app_30/widgets/my_scroll_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:stacked/stacked.dart';
import 'package:url_launcher/url_launcher.dart';


class VipPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    ButtonStyle buildButtonStyle() {
      return ButtonStyle(
        //定义文本的样式 这里设置的颜色是不起作用的
        textStyle: MaterialStateProperty.all(
            TextStyle(fontSize: 15, color: Colours.app_main)),
        //设置按钮上字体与图标的颜色
        //foregroundColor: MaterialStateProperty.all(Colors.deepPurple),
        //更优美的方式来设置
        foregroundColor: MaterialStateProperty.resolveWith(
          (states) {
            if (states.contains(MaterialState.focused) &&
                !states.contains(MaterialState.pressed)) {
              //获取焦点时的颜色
              return Colors.white;
            } else if (states.contains(MaterialState.pressed)) {
              //按下时的颜色
              return Colors.white70;
            }
            //默认状态使用灰色
            return Colors.white;
          },
        ),
        //背景颜色
        backgroundColor: MaterialStateProperty.resolveWith((states) {
          //设置按下时的背景颜色
          if (states.contains(MaterialState.pressed)) {
            return Colours.dark_app_main;
          }
          //默认不使用背景颜色
          return Colours.app_main;
        }),
        //设置水波纹颜色
        overlayColor: MaterialStateProperty.all(Colours.app_main),
        //设置阴影  不适用于这里的TextButton
        elevation: MaterialStateProperty.all(0),
        //设置按钮内边距
        padding: MaterialStateProperty.all(EdgeInsets.all(10)),
        //设置按钮的大小
        minimumSize: MaterialStateProperty.all(Size(160, 40)),
        //设置边框
        // side: MaterialStateProperty.all(BorderSide(color: Colors.grey, width: 1)),
        //外边框装饰 会覆盖 side 配置的样式
        // shape: MaterialStateProperty.all(StadiumBorder()),
      );
    }

    var viewModelBuilder2 = ViewModelBuilder<VipViewModel>.reactive(
        viewModelBuilder: () => VipViewModel(),
        onModelReady: (viewModel) => viewModel.initialise(),
        builder: (context, viewModel, child) => Scaffold(
              appBar: MyAppBar(
                isBack: false,
                centerTitle: viewModel.title,
              ),
              body: MyScrollView(
                children: [
                  Container(
                    decoration: BoxDecoration(
                      color: Colors.white10,
                    ),
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Container(
                            decoration: BoxDecoration(
                              image: new DecorationImage(
                                fit: BoxFit.fill,
                                image: new AssetImage('assets/image/vipbg.png'),
                              ),
                            ),
                            child: Padding(
                                padding: EdgeInsets.all(10),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Row(
                                      mainAxisSize: MainAxisSize.max,
                                      children: [
                                        Image.asset(
                                          "assets/image/vip_finger.png",
                                          width: 13,
                                          height: 13,
                                        ),
                                        Padding(
                                          padding: EdgeInsets.only(left: 5),
                                          child: Text(
                                            "注意",
                                            textAlign: TextAlign.center,
                                            style: TextStyle(
                                                fontSize: 16,
                                                color: Colors.white),
                                          ),
                                        )
                                      ],
                                    ),
                                    Text(
                                      "1-VIP会员每日收益无上限，赚钱速度更快;\n2-开通VIP会员，取消普通会员提现限制，独立服务器更加稳定;\n3-如果需要免费开通VIP，请进入App的分享界面；   \n4-充值后自动开通VIP，请记得备注填写自己的账号；\n5-充值时，软件退出，重新登录即可升级VIP会员；",
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                          fontSize: 14, color: Colors.white),
                                    )
                                  ],
                                )),
                          ),
                          Container(
                            decoration: BoxDecoration(
                              color: Colors.black,
                            ),
                            child: Padding(
                                padding: EdgeInsets.all(10),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Row(
                                      mainAxisSize: MainAxisSize.max,
                                      children: [
                                        Expanded(
                                          child: TextButton(
                                            child: Text("开通vip"),
                                            //添加一个点击事件
                                            onPressed: () {
                                              if (viewModel.gongGao!.payurl !=
                                                      null &&
                                                  viewModel.gongGao!.payurl!
                                                      .isEmpty) {
                                                showSingleDialog(
                                                  context,
                                                  '为保证资源充足，暂停vip开通!',
                                                );
                                              } else {
                                                if (viewModel.gongGao!.payurl
                                                        ?.replaceAll("[user]",
                                                            getUser()!) !=
                                                    null) {
                                                  String s = viewModel
                                                      .gongGao!.payurl!
                                                      .replaceAll(
                                                          "[user]", getUser()!);
                                                  launch(
                                                      '${Uri.decodeQueryComponent(s)}');
                                                  SystemNavigator.pop;
                                                  exit(0);
                                                }
                                              }
                                            },
                                            //设置按钮是否自动获取焦点
                                            autofocus: true,
                                            //定义一下文本样式
                                            style: buildButtonStyle(),
                                          ),
                                        ),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        Expanded(
                                          child: // use Spacer
                                              TextButton(
                                            child: Text("免费申请vip"),
                                            //添加一个点击事件
                                            onPressed: () {
                                              showMultipleDialog(
                                                context,
                                                '为保证资源充足，暂停vip开通!',
                                                negativeButtonText: '取消',
                                                positiveButtonText: '开通',
                                                positiveAction: () => {
                                                  viewModel.getFreevip(context),
                                                  Navigator.of(context).pop(),
                                                },
                                              );
                                            },
                                            //设置按钮是否自动获取焦点
                                            autofocus: true,
                                            //定义一下文本样式
                                            style: buildButtonStyle(),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ],
                                )),
                          ),
                          Padding(
                              padding: EdgeInsets.only(top: 5, bottom: 5),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                mainAxisSize: MainAxisSize.max,
                                children: [
                                  Container(
                                    child: GestureDetector(
                                      child: Text(
                                        "vip会员与普通会员的区别？点击查看视频",
                                        style: TextStyle(
                                            fontSize: 12, color: Colors.blue),
                                      ),
                                      onTap: () => {
                                        launch('${viewModel.gongGao!.vipspurl}')
                                      },
                                    ),
                                  ),
                                  SizedBox(
                                    width: 10,
                                  )
                                ],
                              )),
                          Image.asset("assets/image/vip.png"),
                          Stack(
                            alignment: AlignmentDirectional.topCenter,
                            children: [
                              Align(
                                alignment: Alignment.center,
                                child: Container(
                                  margin: EdgeInsets.fromLTRB(10, 20, 10, 10),
                                  decoration: BoxDecoration(
                                    color: Colors.black87,
                                    shape: BoxShape.rectangle,
                                    borderRadius: BorderRadius.circular(5.0),
                                  ),
                                  child: Padding(
                                      padding:
                                          EdgeInsets.fromLTRB(10, 15, 10, 10),
                                      child: Text(
                                        "${viewModel.description}",
                                        textAlign: TextAlign.left,
                                        style: TextStyle(
                                            fontSize: 14, color: Colors.white),
                                      )),
                                ),
                              ),
                              Align(
                                alignment: Alignment(0, 0),
                                child: Container(
                                  margin: EdgeInsets.only(top: 10),
                                  padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                  decoration: BoxDecoration(
                                    color: Colours.app_main,
                                    shape: BoxShape.rectangle,
                                    borderRadius: BorderRadius.circular(5.0),
                                  ),
                                  child: Padding(
                                    padding: EdgeInsets.all(3),
                                    child: Row(
                                        mainAxisSize: MainAxisSize.min,
                                        children: [
                                          Text(
                                            "免费vip说明",
                                            textAlign: TextAlign.left,
                                            style: TextStyle(
                                                fontSize: 13,
                                                color: Colors.white),
                                          )
                                        ]),
                                  ),
                                ),
                              )
                            ],
                          )
                        ]),
                  ),
                ],
              ),
            ));
    return viewModelBuilder2;
  }
}
